var mongoose=require('./sqlModels');

var ordersSchema=new mongoose.Schema({
    orderId : String,
    orderTel : String,
    size : Number,
    orderState : String,
    reciveAdress : String,
    id : Number,
    count : Number,
    sum : Number
});
var _date= new Date();

var dateStr=_date.toLocaleDateString()+'-'+_date.getHours()+'-'+_date.getMinutes()+'-'+_date.getSeconds();
var pattern=/-/g;

var orderId=dateStr.replace(pattern,'');

var order={
    orderId:orderId,
    orderTel:18697721200,
    size:2,
    orderState:'waitPaid',
    reciveAdress:'上海市卢湾区',
    id:3,
    count:2,
    sum:400
};

var ordersModel=mongoose.model('orderlist',ordersSchema,'orderlist');
//获取

exports.getOrder=function(callback){
    ordersModel.find(function(err,docs){
        if(err){
            throw err;
        }else{
            callback(docs);
        }
    })
};
//添加
exports.orderAdd = function (order,callback) {
    ordersModel.create(order,function (err) {
        if(err)
            callback(false);
        else
            callback(true);
    })
};
//删除



exports.orderDelete = function (orderId,callback) {
    console.log("orderId",orderId);
    //测试用
        var condition={orderId:'2017831222854'};
    ordersModel.remove(condition,function (err,doc) {
        if(err){
            callback(false);
        }else {
            console.log(doc);
        }
    })
};




/*exports.orderEdit = function (condition,updateData,callback) {
    goodsModel.update(condition,updateData, {multi: true,overwrite:true},function (err) {
        if(err){
            callback(false)
        }else {
            callback(true)
        }
    })
};*/


//获取

exports.getCart=function(callback){
    var getCondition={orderId:'',orderTel: "18697721218"};
    ordersModel.find(getCondition,function(err,docs){
        if(err){
            throw err;
        }else{
            callback(docs);
        }
    })
};
//添加
exports.addCart = function (callback) {
    ordersModel.create(cart,function (err) {
        if(err)
            callback(false);
        else
            callback(true);
    })
};
//删除



exports.deleteCart = function (callback) {
    //console.log("orderId",orderId)
   /* var condition={"orderId" : "",
        "orderTel" : "18697721218",
        "id" : 8};//测试用 true 数据已删*/
    ordersModel.remove(condition,function (err,doc) {
        if(err){
            callback(false);
        }else {
            callback(true);
        }
    })
};


